package it.apiconsulting.wot.service;

import java.util.ArrayList;
import java.util.List;

import it.apiconsulting.wot.bean.FoodBean;
import it.apiconsulting.wot.bean.SchedaFoodBean;
import it.apiconsulting.wot.controller.FoodManager;
import it.apiconsulting.wot.controller.SchedaFoodManager;
import it.apiconsulting.wot.model.Food;
import it.apiconsulting.wot.model.SchedaFood;

public class SchedaUtenteService {
	
	private SchedaFoodManager schedaFoodManager = new SchedaFoodManager();
	private FoodManager foodManager = new FoodManager();
	
	public List<SchedaFoodBean> getSchedeFoodBean(String idUtente, String numSchede) {
		
		List<SchedaFoodBean> schedeFoodBean = new ArrayList<SchedaFoodBean>();
		try {
			
			List<Long> idSchede = schedaFoodManager.listSchedeByIdUtente(Long.parseLong(idUtente), Integer.parseInt(numSchede));
			for ( Long idScheda : idSchede ) {
				
				SchedaFoodBean sfBean = new SchedaFoodBean(Long.parseLong(idUtente), idScheda);
				
				List<FoodBean> foodBeanList = new ArrayList<FoodBean>();
				for ( SchedaFood schedaFood : schedaFoodManager.listByIdScheda(idScheda) ) {
					
					sfBean.setSchedaDate(schedaFood.getSchedaDate());
					
					Food food = foodManager.getFoodById(schedaFood.getIdFood());
					if ( food!=null ) {
						FoodBean fBean = new FoodBean();
						fBean.setId(food.getId());
						fBean.setNome(food.getNome());
						fBean.setQnt(food.getQnt());
						fBean.setQntUnita(food.getQntUnita());
						fBean.setQntDescrizione(food.getQntDescrizione());
						fBean.setPunti(food.getPunti());
						
						foodBeanList.add(fBean);
					}
				}
				sfBean.setFoodBeanList(foodBeanList);
				
				schedeFoodBean.add(sfBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return schedeFoodBean;
	}
}
